<?php
   class media_song{
    private $template;
    private $util;
    private $db;
    //variable
    private $act;
    private $id;
   
    public function __construct(){
        require_once 'code/util.class.php'; 
        require_once("code/pagination.class.php");    
        $this->util = new util();
        $this->db = new db();
    }
    
    public function autorun(){
        $this->act =  isset($_GET['act'])?$_GET['act']:'list'; 
        $this->id = isset($_GET['id'])?$_GET['id']:0;   
        switch($this->act){
            case 'allSong':
                $this->get_all_singer();
                break;
            case 'listfeature':
                $this->get_list_feature();
                break;
            case 'listnewest':
                $this->get_list_top();
                break;
            case 'play':
                $this->play_song();
                break;
            case 'albumdetail':
                $this->album_detail();
                break;
             case 'albumselect':
                $this->album_select();
                break;
            case 'detail':
                $this->get_song_detail();
                break;
            case 'genrelist':
                $this->genre_list();
                break;
            case 'search':
                $this->search();
                break;
            case 'paging':
                $this->get_paging_newest();
                break;
            case 'display':
                $this->display();
                break;
            default:
                $this->get_list_top(); 
                break;   
            
        }
          
    }
    //-------------------------------------------
    private function get_song_detail(){
        $this->db->openconnection();
        $song = $this->db->run_select_by_id('song_singer',$this->id);
        $this->db->closeconnection();
        //
        $nSong = new media_song();
        $sql = "SELECT * FROM song_singer WHERE singerid =$song[singerid] and id <> $song[id]";
       
        $this->db->openconnection();
        $songs_more = $nSong->db->run_query($sql);
        $this->db->closeconnection();
        //
        
        $this->util->send_var('songItem',$song);
          $this->util->send_var('songs_more',$songs_more); 
         return ($this->util->generate_page('../templates/media-frontend/song/song_detail.tpl'));    
    }
    
    //-------------------------------------------
      //Get feature list to index page
    private  function genre_list(){
        $sql = "SELECT * FROM songs WHERE typeid='$this->id'";
        $this->db->openconnection();
        
        $song = $this->db->run_query($sql);
        $this->db->closeconnection();
         $this->util->send_var('songs_newest',$song);    
         return ($this->util->generate_page('../templates/media-frontend/song/list_newest.tpl'));       
    }
    //Get feature list to index page
    public function get_list_feature(){
        $sql = "SELECT * FROM song_singer WHERE feature='1' AND status='1' LIMIT 0,15 ";
        
        $this->db->openconnection();
        
        $song = $this->db->run_query($sql);
        $this->db->closeconnection();
        
        return $song;        
    }
     //Get newest list to index page
    public function get_list_newest(){
        $sql = "SELECT * FROM song_singer WHERE status='1' ORDER BY `createdon` DESC LIMIT 0,15 ";
        $this->db->openconnection();
        $song = $this->db->run_query($sql);
        $this->db->closeconnection();
        
        return $song;        
    }
    //Get top 10 played songs
    public function get_top10(){
        $sql = "SELECT * FROM song_singer WHERE status='1' ORDER BY `listentcount` DESC LIMIT 0,10 ";
        $this->db->openconnection();
        $song = $this->db->run_query($sql);
        $this->db->closeconnection();
        
        return $song;        
    }
    //Play song and buid playlist
    private function play_song(){
        
         return ($this->util->generate_page('../templates/media-frontend/media-player/play_list.tpl')); 
    }
    
    private function display(){
       
        require_once("media-frontend/module/album.php");
        $album = new media_album();
        $song = new media_song();
        $album_data = $album->get_list_newest();  
         $fea = $song->get_list_feature();  
          
        
         //print $this->get_paging_newest(); 
        $this->module = 'new_songs'; 
        $this->util->send_var('module',$this->module);
        $this->util->send_var('newalbums',$album_data); 
        $this->util->send_var('songs_feature',$fea);    
        $this->util->send_var('newestSongs',$this->get_paging_newest());
            
        return($this->util->generate_page('../templates/media-frontend/song/song.tpl'));
    }  
    
    private function get_paging_newest(){
        $str = "SELECT * FROM song_singer WHERE status='1' ORDER BY `createdon` DESC"; 
        $this->db->openconnection();
        $newsongs = $this->db->run_query($str);
        $this->db->closeconnection();  
       ///------------------------------
        // Create the pagination object
        $pagination = new pagination;
        $newSongPages = $pagination->generate($newsongs,10);
        
        //-------------------------------------------  
        $this->util->send_var('songs_newest',$newSongPages);  
        $this->util->send_var('pagination',$pagination->links()); 
       
        if($this->act=='paging'){
            $pageCurr =  isset($_GET['page'])?$_GET['page']:1;
            $startNum = 10*($pageCurr-1) + 1;
            $this->util->send_var('currNum',$startNum); 
           return($this->util->generate_page('../templates/media-frontend/song/list_newest.tpl')); 
        }
        else
        { 
            return($this->util->load_page('../templates/media-frontend/song/list_newest.tpl')); 
        }    
    }
    
    
    //------------------------------------------------------------------------------------------------
    private function album_detail() {
        $spt = "SELECT * FROM songs
                INNER JOIN album_songs ON songs.id = album_songs.sid
                WHERE album_songs.aid =$this->id";
        $this->db->openconnection();
        $newsongs = $this->db->run_query($spt);
        $albumdetail = $this->db->run_select_by_id('album',$this->id);
        $this->db->closeconnection();
        $this->util->send_var('songs_newest',$newsongs);
        $this->util->send_var('albumdetail',$albumdetail);
        return($this->util->generate_page('../templates/media-frontend/song/album_detail.tpl'));
    }
      private function album_select() {
        $spt = "SELECT * FROM songs
                INNER JOIN album_songs ON songs.id = album_songs.sid
                WHERE album_songs.aid =$this->id";
        $this->db->openconnection();
        $newsongs = $this->db->run_query($spt);
        $albumdetail = $this->db->run_select_by_id('album',$this->id);
        $this->db->closeconnection();
          require_once("media-frontend/module/album.php");
        $album = new media_album();
        $album_data = $album->get_list_newest();  
        $this->util->send_var('songs_newest',$newsongs);
        $this->util->send_var('albumdetail',$album_data);
        return($this->util->generate_page('../templates/media-frontend/song/song.tpl'));
    }
    private function search(){
        $value = isset($_POST['txtSearch'])?$_POST['txtSearch']:'';
        $option = isset($_POST['r1'])?$_POST['r1']:1;
        print $option;
        if($value != ''){
            if($option==1){
                $sql = "SELECT * FROM song_singer WHERE title like '%$value%'";
            }
            else
            {
                $sql = "SELECT * FROM song_singer WHERE name like '%$value%'";    
            }
            $this->db->openconnection();
            
            $song = $this->db->run_query($sql);
            $this->db->closeconnection();
            $this->util->send_var('songs_newest',$song);    
        } 
        require_once("media-frontend/module/album.php");
        $album = new media_album();
        $album_data = $album->get_list_newest(); 
        $module = 'search_result'; 
        $this->util->send_var('module',$module);
        $this->util->send_var('newalbums',$album_data);    
        return ($this->util->generate_page('../templates/media-frontend/song/song.tpl'));       
   }
} 
?>
